package com.hifar.test.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.enums.ReportTypeEnum;
import com.hifar.log.AutoLogUtils;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.test.template.pojo.HfEnvTemplate;
import com.hifar.test.template.pojo.VO.HfEnvTemplateVO;
import com.hifar.test.template.service.IHfEnvTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhuWeiWei
 * @description 模板管理
 * @date 2024/12/27 15:30
 */
@RestController
@RequestMapping("/api/busi/test/envTemplate")
@Api("模板管理")
public class HfEnvTemplateController {

    @Autowired
    private IHfEnvTemplateService templateService;

    @Autowired
    private AutoLogUtils autoLogUtils;

    @ApiOperation("模板分类查询")
    @GetMapping("/listTemplateClassify")
    public BusiResponse listTemplateClassify() {
        List<Map<String, Object>> mapList = ReportTypeEnum.listData();
        return BusiResponse.OK(mapList);
    }

    @ApiOperation("模板管理:列表查询")
    @PostMapping("/listPage")
    public BusiResponse listPage(@RequestBody QueryParamsVO queryParams) {
        QueryParamBean<HfEnvTemplate> queryParamBean = new QueryParamBean(queryParams, HfEnvTemplate.class);
        Page<HfEnvTemplate> page = queryParamBean.getPage();
        QueryWrapper<HfEnvTemplate> queryWrapper = queryParamBean.getQueryWrapper();
        Page<HfEnvTemplate> pageData = templateService.page(page, queryWrapper);
        /*计算分页信息返回给前端*/
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }

    @ApiOperation("模板管理:新增")
    @PostMapping("/add")
    public BusiResponse add(@RequestBody @Validated HfEnvTemplateVO envTemplate) {
        templateService.saveTemplate(envTemplate);
        String templateName = envTemplate.getTemplateName();
        autoLogUtils.addLog("环境试验-模板管理", String.format("新增模板管理:【%s】", templateName));

        return BusiResponse.OK();
    }

    @ApiOperation("模板管理:编辑")
    @PostMapping("/edit")
    public BusiResponse edit(@RequestBody @Validated HfEnvTemplateVO envTemplate) {
        templateService.editTemplate(envTemplate);
        String templateName = envTemplate.getTemplateName();
        autoLogUtils.modifyLog("环境试验-模板管理", String.format("编辑模板管理:【%s】", templateName));
        return BusiResponse.OK();
    }

    @ApiOperation("模板管理:删除")
    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> ids) {
        templateService.removeByIds(ids);
        autoLogUtils.deleteLog("环境试验-模板管理", String.format("删除%d条模板管理", ids.size()));
        return BusiResponse.OK();
    }


    @ApiOperation("模板管理:查询详情")
    @GetMapping("/queryById")
    public BusiResponse queryById(@RequestParam("id") String id) {
        HfEnvTemplateVO templateVO = templateService.queryVoById(id);
        return BusiResponse.OK(templateVO);
    }

}
